package pt.ipleiria.estg.es2.byinvitationonly;

import android.content.Context;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.util.AttributeSet;

/**
 * Created by EduardoPortatil on 13-03-2015.
 */
public class MyEditTextPreference extends EditTextPreference {

    public MyEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        definirListener();
    }

    public MyEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        definirListener();
    }

    public MyEditTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        definirListener();
    }

    public MyEditTextPreference(Context context) {
        super(context);
        definirListener();
    }

    public CharSequence getSummary(){
        if(getEditText().getText().toString().isEmpty()){
            return super.getSummary();
        }

        return getEditText().getText().toString();
    }

    private void definirListener(){
        setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                if(newValue.toString().isEmpty()){
                    setSummary(R.string.introduzir_email);
                }else {
                    setSummary(newValue.toString());
                }
                return false;
            }
        });

    }

}
